using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;

namespace MapReduce
{
    public class U
    {
        //public const double DEFAULT_CHECK_INTERVAL = 1000 * 60 * 2; //2 minutes
        public const ushort DEFAULT_MASTER_PORT = 8080;
        public const ushort DEFAULT_SLAVE_PORT = 8081;
        public const ushort DEFAULT_HTTP_PORT = 8082;


        
        public static String MapPath(String pPth)
        {
            String strPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            return Path.Combine(strPath, pPth);
        }
        
        public static void Log(Object pO)
        {
            Console.WriteLine(pO);

        }

        public static byte[] GetBytes(Object s)
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(stream, s);

            return stream.ToArray();
        }
        public static byte[] GetBytes(List<String> s)
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(stream, s);

            return stream.ToArray();
        }
        public static Object Deserialize(byte []b)
        {
            MemoryStream stream = new MemoryStream(b);
            BinaryFormatter bf = new BinaryFormatter();
            return bf.Deserialize(stream);
        }
        public static byte[] Serialize(Object o)
        {
            MemoryStream opStream = new MemoryStream();
            BinaryFormatter opFormatter = new BinaryFormatter();

            opFormatter.Serialize(opStream, o);

            return opStream.ToArray();
        }

        public static byte[] GetBytes(String s)
        {
            if (s == null) throw new Exception("NULL String ");
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            return (encoding.GetBytes(s));
        }
        public static StringBuilder Convert(byte []bb)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bb.Length; i++)
            {
                sb.Append(System.Convert.ToChar(bb[i]));
            }

            return sb;
        }


    }
}
